//
//  ChangePWViewController.swift
//  DriverClock
//
//  Created by 童巍 on 17/1/16.
//  Copyright © 2017年 童巍. All rights reserved.
//

import UIKit

//func lengthOfPx(px: CGFloat)->CGFloat{
//    return ((px/1080.0)*UIScreen.main.bounds.size.width)
//}
//
//func systemFontOfpx(px:CGFloat) -> UIFont{
//    
//    return UIFont.systemFont(ofSize: ((px/2/96.0) * 72.0));
//}

class FogetPWViewController: UIViewController {
    @IBOutlet weak var viewOne: UIView!
    @IBOutlet weak var viewTwo: UIView!
    @IBOutlet weak var viewThree: UIView!
    @IBOutlet weak var viewFour: UIView!
    @IBOutlet weak var viewOneImage: UIImageView!
    @IBOutlet weak var viewOneTF: UITextField!
    @IBOutlet weak var viewTwoImage: UIImageView!
    @IBOutlet weak var viewTwoTF: UITextField!
    @IBOutlet weak var getCode: UIButton!
    @IBOutlet weak var viewThreeImage: UIImageView!
    @IBOutlet weak var viewThreeTF: UITextField!
    @IBOutlet weak var vierFourImage: UIImageView!
    @IBOutlet weak var viewFourTF: UITextField!
    @IBOutlet weak var sureButton: UIButton!
    var seconds:Int = 0;
    var backgroundDate:NSDate?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.removeConstraints(self.view.constraints);
        initUI()
        self.seconds = 300;
        // Do any additional setup after loading the view.
        
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnterForeground) name:@"EnterForeground" object:nil];
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(EnterBackground) name:@"EnterBackground" object:nil];
        NotificationCenter.default.addObserver(self, selector:#selector(EnterForeground), name: NSNotification.Name(rawValue: "EnterForeground"), object: nil)
        NotificationCenter.default.addObserver(self, selector:#selector(EnterBackground), name: NSNotification.Name(rawValue: "EnterBackground"), object: nil)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = false;
    }
    func EnterForeground(){
        
        let timeGone:TimeInterval = self.backgroundDate!.timeIntervalSinceNow
        let timeGoneInt : Int = Int(timeGone)
        self.seconds = self.seconds + timeGoneInt;
    }
    func EnterBackground(){

        let date = NSDate()
        
        self.backgroundDate = date
    }
    
    func initUI() {
         self.viewOne.mas_makeConstraints { (make) in
            make?.width.mas_equalTo()(lengthOfPx(px: 820));
            make?.height.mas_equalTo()(lengthOfPx(px: 120));
            make?.centerX.mas_equalTo()(self.view.mas_centerX);
            make?.top.mas_equalTo()(lengthOfPx(px: 500));

         }
        self.viewOne.layer.cornerRadius = lengthOfPx(px: 60);
        self.viewOne.clipsToBounds = true;
        self.viewOne.layer.borderColor = UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1).cgColor
        self.viewOne.layer.borderWidth = 1;
        
        self.viewTwo.mas_makeConstraints { (make) in
            make?.width.mas_equalTo()(lengthOfPx(px: 820));
            make?.height.mas_equalTo()(lengthOfPx(px: 120));
            make?.centerX.mas_equalTo()(self.view.mas_centerX);
            make?.top.mas_equalTo()(lengthOfPx(px: (500 + 170)));
            
        }
        self.viewTwo.layer.cornerRadius = lengthOfPx(px: 60);
        self.viewTwo.clipsToBounds = true;
        self.viewTwo.layer.borderColor = UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1).cgColor
        self.viewTwo.layer.borderWidth = 1;
        
        self.viewThree.mas_makeConstraints { (make) in
            make?.width.mas_equalTo()(lengthOfPx(px: 820));
            make?.height.mas_equalTo()(lengthOfPx(px: 120));
            make?.centerX.mas_equalTo()(self.view.mas_centerX);
            make?.top.mas_equalTo()(lengthOfPx(px: (500 + 170 + 170)));
            
        }
        self.viewThree.layer.cornerRadius = lengthOfPx(px: 60);
        self.viewThree.clipsToBounds = true;
        self.viewThree.layer.borderColor = UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1).cgColor
        self.viewThree.layer.borderWidth = 1;

        
        self.viewFour.mas_makeConstraints { (make) in
            make?.width.mas_equalTo()(lengthOfPx(px: 820));
            make?.height.mas_equalTo()(lengthOfPx(px: 120));
            make?.centerX.mas_equalTo()(self.view.mas_centerX);
            make?.top.mas_equalTo()(lengthOfPx(px: (500 + 170 + 170 + 170)));
            
        }
        self.viewFour.layer.cornerRadius = lengthOfPx(px: 60);
        self.viewFour.clipsToBounds = true;
        self.viewFour.layer.borderColor = UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1).cgColor
        self.viewFour.layer.borderWidth = 1;


/** imageLayout ***/
        self.viewOneImage.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.centerY.mas_equalTo()(self.viewOne.mas_centerY);
            make?.width.mas_equalTo()(lengthOfPx(px: 60));
            make?.height.mas_equalTo()(lengthOfPx(px: 60));
        }
        
        self.viewTwoImage.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.centerY.mas_equalTo()(self.viewTwo.mas_centerY);
            make?.width.mas_equalTo()(lengthOfPx(px: 60));
            make?.height.mas_equalTo()(lengthOfPx(px: 60));
        }
        
        self.viewThreeImage.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.centerY.mas_equalTo()(self.viewThree.mas_centerY);
            make?.width.mas_equalTo()(lengthOfPx(px: 60));
            make?.height.mas_equalTo()(lengthOfPx(px: 60));
        }
        
        self.vierFourImage.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 50));
            make?.centerY.mas_equalTo()(self.viewFour.mas_centerY);
            make?.width.mas_equalTo()(lengthOfPx(px: 60));
            make?.height.mas_equalTo()(lengthOfPx(px: 60));
        }
        
/*** textfieldlayout ***/
        self.viewOneTF.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 140));
            make?.right.mas_equalTo()(lengthOfPx(px: -80))
            make?.centerY.mas_equalTo()(self.viewOne.mas_centerY);
            make?.height.mas_equalTo()(self.viewOne.mas_height)
        }
        self.viewOneTF.attributedPlaceholder = NSAttributedString.init(string: "请输入手机号码", attributes: [NSFontAttributeName:systemFontOfpx(px: 48),NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1)])
        
        
        self.viewTwoTF.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 140));
            make?.right.mas_equalTo()(lengthOfPx(px: -320))
            make?.centerY.mas_equalTo()(self.viewTwo.mas_centerY);
            make?.height.mas_equalTo()(self.viewTwo.mas_height)
        }
         self.viewTwoTF.attributedPlaceholder = NSAttributedString.init(string: "请输入验证码", attributes: [NSFontAttributeName:systemFontOfpx(px: 44),NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1)])

        
        self.viewThreeTF.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 140));
            make?.right.mas_equalTo()(lengthOfPx(px: -80))
            make?.centerY.mas_equalTo()(self.viewThree.mas_centerY);
            make?.height.mas_equalTo()(self.viewThree.mas_height)
        }
         self.viewThreeTF.attributedPlaceholder = NSAttributedString.init(string: "请输入新密码", attributes: [NSFontAttributeName:systemFontOfpx(px: 48),NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1)])

        
        self.viewFourTF.mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(lengthOfPx(px: 140));
            make?.right.mas_equalTo()(lengthOfPx(px: -80))
            make?.centerY.mas_equalTo()(self.viewFour.mas_centerY);
            make?.height.mas_equalTo()(self.viewFour.mas_height)
        }
        self.viewFourTF.attributedPlaceholder = NSAttributedString.init(string: "请确认新密码", attributes: [NSFontAttributeName:systemFontOfpx(px: 48),NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 191/255.0, green: 207/255.0, blue: 229/255.0, alpha: 1)])
        
        self.sureButton.mas_makeConstraints { (make) in
            make?.width.mas_equalTo()(lengthOfPx(px: 820));
            make?.height.mas_equalTo()(lengthOfPx(px: 120));
            make?.centerX.mas_equalTo()(self.view.mas_centerX);
            make?.top.mas_equalTo()(self.viewFour.mas_bottom)?.setOffset(lengthOfPx(px: 160));
        }
        self.sureButton.layer.cornerRadius = lengthOfPx(px: 60)
        self.sureButton.titleLabel?.font = systemFontOfpx(px: 54);
        
        self.getCode.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(0);
            make?.top.mas_equalTo()(0);
            make?.bottom.mas_equalTo()(0);
            make?.width.mas_equalTo()(lengthOfPx(px: 300))
        }
        self.getCode.layer.cornerRadius = lengthOfPx(px: 60)
        self.getCode.titleLabel?.font = systemFontOfpx(px: 40)
        
        
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @available(iOS 10.0, *)
    @IBAction func getPhoneCode(_ sender: UIButton) {
        
        if !Tool.valiMobile(viewOneTF.text){
            Tool.error(withMsg: "请输入正确的手机号")
            return
        }
       
        sender.isEnabled = false
        Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block:{(timer) -> Void in
            self.seconds-=1
            sender.setTitle("(\(self.seconds))", for: UIControlState.normal)
            if self.seconds <= 0{
                sender.isEnabled = true
                sender.setTitle("重新获取", for: UIControlState.normal)
                timer.invalidate()
                self.seconds = 300
            };
        })

        
        
        WTMNetworkTool.shared().request(withUrlstring: "app/getVerificationCode", method: "POST", parameters: ["params":["mobileNumber":self.viewOneTF.text!,"purpose":"2"]], success: {(respone)->Void in
        Tool.error(withMsg: (respone as? NSDictionary)?.object(forKey: "resultMsg") as! String!)
            let status = (respone as? NSDictionary)?.object(forKey: "status") as? NSNumber
            if status?.intValue == 0 {
            }else{
               
            }
        }, failure: {(error)->Void in
            UIAlertView.init(title: "获取失败", message: "请检查网络", delegate: nil, cancelButtonTitle: "确定").show()
            sender.setTitle("重新获取", for: UIControlState.normal)
            sender.isEnabled = true
        })
        
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        viewOneTF.resignFirstResponder()
        viewTwoTF.resignFirstResponder()
        viewThreeTF.resignFirstResponder()
        viewFourTF.resignFirstResponder()
    }
    
    

    @IBAction func fogetPS(_ sender: UIButton) {
        sender.isEnabled = false
        if (viewOneTF.text?.isEmpty)!||(viewTwoTF.text?.isEmpty)!||(viewThreeTF.text?.isEmpty)!||(viewFourTF.text?.isEmpty)! {
            Tool.error(withMsg:"资料不完整")
            sender.isEnabled = true

            return
        }
        
        if viewThreeTF.text == viewFourTF.text{
        
            WTMNetworkTool.shared().request(withUrlstring: "app/user/forgetPassword", method: "POST", parameters: ["params":["mobile":self.viewOneTF.text!,"verificationCode":self.viewTwoTF.text!,"newpassword":Tool.md5(self.viewFourTF.text!)]], success: {(respone)->Void in
                Tool.error(withMsg: (respone as? NSDictionary)?.object(forKey: "resultMsg") as! String!)
                sender.isEnabled = true
                self.navigationController?.popViewController(animated: true)
            }, failure: {(error)->Void in
                sender.isEnabled = true

            })

        }else{
            Tool.error(withMsg: "两次密码不一致")
            sender.isEnabled = true
        
        }
        
        
    }
   
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
